package com.starblink.android.basic.ext

import android.content.Context
import android.view.View
import androidx.viewbinding.ViewBinding
import androidx.viewpager.widget.ViewPager
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.util.FontUtil
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView


/**
 *
 */
fun MagicIndicator.bindSimpleTab(
    tabs: List<String>,
    gap: Int = 12.dp2px(),
    normalColor: Int = color(com.starblink.basic.style.R.color.white40),
    selectedColor: Int = color(com.starblink.basic.style.R.color.sk_gray0),
    textSize: Float = 16f,
    indicatorWidth: Int = 24.dp2px(),
    indicatorHeight: Int = 3.dp2px(),
    viewPager: ViewPager? = null,
    indicator: Int = color(com.starblink.basic.style.R.color.gu_text_brand600),
    trackCall: ((view: View, index: Int) -> Unit)? = null,
    onTabSelect: ((index: Int) -> Unit)? = null,
) {
    val commonNavigator = CommonNavigator(context)
    commonNavigator.adapter = object : CommonNavigatorAdapter() {
        override fun getCount(): Int = tabs.size

        override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
            val view = ColorTransitionPagerTitleView(context)
            view.normalColor = normalColor
            view.selectedColor = selectedColor
            view.textSize = textSize
            view.text = tabs[index]
            view.typeface = FontUtil.getTypeface(FontUtil.MONTSERRAT_MEDIUM) // 设置字体
            //view.paint.isFakeBoldText = true
            view.setPadding(
                gap.shr(1),
                view.paddingTop,
                gap.shr(1),
                view.paddingBottom
            )
            view.setOnClickListener {
                viewPager?.currentItem = index
                onTabSelect?.invoke(index)
            }
            trackCall?.invoke(view, index)
            FontUtil.setMontseratSemiBold(view)
            return view
        }

        //新版本为四圆角矩形 去除了Y轴偏移量，如需要，请自行判断 -->高度增加一倍，再往下偏移
        override fun getIndicator(context: Context?): IPagerIndicator {
            return LinePagerIndicator(context).apply {
                mode = LinePagerIndicator.MODE_EXACTLY
                lineWidth = indicatorWidth.toFloat()
                roundRadius = indicatorHeight / 2f
                lineHeight = indicatorHeight.toFloat()
                setColors(indicator)
//              yOffset = -indicatorHeight.toFloat()
            }
        }
    }
    navigator = commonNavigator
    viewPager?.let {
        ViewPagerHelper.bind(this, it)
    }

}


fun<VB: ViewBinding> MagicIndicator.bindTab(
    count: Int,
    indicatorBuilder: (() -> IPagerIndicator)? = null,
    viewPager: ViewPager? = null,
    gap: Int = 0,
    edgeGap: Int = 0,
    onDeselected: ((index: Int, binding: VB) -> Unit)? = null,
    onTabSelect: ((index: Int, binding: VB) -> Unit)? = null,
    tabViewBuilder: ((index: Int) -> VB),
) {
    val commonNavigator = CommonNavigator(context)
    commonNavigator.adapter = object : CommonNavigatorAdapter() {
        override fun getCount(): Int = count
        override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
            val view = CommonPagerTitleView(context)
            val binding = tabViewBuilder.invoke(index)

            view.setPadding(if (index == 0) edgeGap else gap, 0, if (index == count - 1) edgeGap else 0, 0)
            view.addView(binding.root)
            binding.root.setOnClickListener {
                onPageSelected(index)
                viewPager?.setCurrentItem(index, false)
            }
            view.onPagerTitleChangeListener = object :
                CommonPagerTitleView.OnPagerTitleChangeListener {
                override fun onSelected(index: Int, totalCount: Int) {
                    onTabSelect?.invoke(index, binding)
                }

                override fun onDeselected(index: Int, totalCount: Int) {
                    onDeselected?.invoke(index, binding)
                }

                override fun onLeave(
                    index: Int,
                    totalCount: Int,
                    leavePercent: Float,
                    leftToRight: Boolean
                ) {

                }

                override fun onEnter(
                    index: Int,
                    totalCount: Int,
                    enterPercent: Float,
                    leftToRight: Boolean
                ) {

                }

            }
            return view
        }

        override fun getIndicator(context: Context?): IPagerIndicator? {
            return indicatorBuilder?.invoke()
        }
    }
    commonNavigator.isFollowTouch = false
    navigator = commonNavigator
    viewPager?.let {
        ViewPagerHelper.bind(this, it)
    }

}